package cn.wolfcode.rbac.service.Impl;

import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.mapper.DepartmentMapper;
import cn.wolfcode.rbac.qo.DepartmentQo;
import cn.wolfcode.rbac.service.DepartService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiahuihui
 * #Description DepartServiceImpl
 * #Date: 2022/7/2 13:26
 */
@Service
public class DepartServiceImpl implements DepartService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> findDepartmentAll() {
        List<Department> departments = departmentMapper.selectAll();
        return departments;
    }

    @Override
    public PageInfo<Department> findByPage(DepartmentQo qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Department> departments = departmentMapper.selectByCondition(qo);
        return new PageInfo<Department>(departments);
    }

    @Override
    public void saveDepartment(Department department) {
        //区分当前操作是修改还是添加
        if(department.getId() == null){
            //添加
            if (department.getName()!=null && department.getName()!="" && department.getSn()!=null && department.getSn()!=""){
                //添加部门
                departmentMapper.insert(department);
            }
        }else{
            //修改
            departmentMapper.updateByPrimaryKey(department);
        }


    }

    @Override
    public void deleteDepartment(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Department findById(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }
}
